/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kaytto1;

import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.IOException;
import javax.microedition.midlet.*;

/**
 * @author Joteitti
 */
public class HomeService extends MIDlet implements ActionListener {

    public static final String DEFAULT_RESOURCES = "/resources.res";
    public static final String DEFAULT_THEME = "Theme";
    public static final int DEFAULT_TRANSITION = CommonTransitions.SLIDE_HORIZONTAL;
    public static final int DEFAULT_TRANSITION_DURATION = 250;

    private boolean paused = false;

    // Forms and commands
    private HomeForm homeForm;
    private BasicForm basicForm;
    private CameraForm cameraForm;
    private ImageForm imageForm;
    private ConfirmationForm confirmationForm;
    private SendForm sendForm;
    private Command startCommand;
    private Command backCommand;
    private Command continueCommand;
    private Command exitCommand;
    private Command takePictureCommand;
    private Command sendCommand;
    private Command newCommand;

    public void startApp() {
        if (paused) {
            resumeMidlet();
        } else {
            initializeMidlet();
            startMidlet();
        }
        paused = false;
    }

    public void pauseApp() {
        paused = true;
    }

    public void destroyApp(boolean unconditional) {
    }

    public void initializeMidlet() {
        // Initialize LWUIT
        Display.init(this);
        try {
            // Load theme
            Resources resources = Resources.open(DEFAULT_RESOURCES);
            UIManager.getInstance().setThemeProps(resources.getTheme(DEFAULT_THEME));
        } catch (IOException e) {
            showExitDialog(e.getMessage(), e.toString());
        }

        // Create forms
        homeForm = new HomeForm(this);
        basicForm = new BasicForm(this);
        cameraForm = new CameraForm(this);
        imageForm = new ImageForm(this);
        confirmationForm = new ConfirmationForm(this);
        sendForm = new SendForm(this);
    }
    
    public void startMidlet() {
        // Switch to home form
        switchForm(homeForm, false);
    }

    public void resumeMidlet() {
        // TODO Resume midlet
    }

    public void exitMidlet() {
        // Destroy application
        destroyApp(true);
        notifyDestroyed();
    }

    public void actionPerformed(ActionEvent event) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public HomeForm getHomeForm() {
        return homeForm;
    }

    public BasicForm getBasicForm() {
        return basicForm;
    }

    public CameraForm getCameraForm() {
        return cameraForm;
    }

    public ImageForm getImageForm() {
        return imageForm;
    }

    public ConfirmationForm getConfirmationForm() {
        return confirmationForm;
    }

    public SendForm getSendForm() {
        return sendForm;
    }

    public Command getStartCommand() {
        // Lazy initialization
        if(startCommand == null) {
            startCommand = new Command(Text.getString(Text.TEXT_GLOBAL_START)) {

                public void actionPerformed(ActionEvent evt) {
                    switchForm(basicForm, false);
                }

            };
        }

        return startCommand;
    }

    public Command getBackCommand() {
        // Lazy initialization
        if(backCommand == null) {
            backCommand = new Command(Text.getString(Text.TEXT_GLOBAL_BACK)) {

                public void actionPerformed(ActionEvent evt) {
                    if(getCurrentForm() == basicForm) {
                        switchForm(homeForm, true);
                    } else if(getCurrentForm() == cameraForm) {
                        switchForm(basicForm, true);
                    } else if(getCurrentForm() == imageForm) {
                        switchForm(cameraForm, true);
                    } else if(getCurrentForm() == confirmationForm) {
                        switchForm(imageForm, true);
                    }
                }

            };
        }

        return backCommand;
    }

    public Command getContinueCommand() {
        // Lazy initialization
        if(continueCommand == null) {
            continueCommand = new Command(Text.getString(Text.TEXT_GLOBAL_CONTINUE)) {

                public void actionPerformed(ActionEvent evt) {
                    Form currentForm = getCurrentForm();
                    if(currentForm == basicForm) {
                        if(basicForm.continueable()) {
                            switchForm(cameraForm, false);
                        } else {
                            showErrorDialog(Text.getString(Text.TEXT_GLOBAL_ERROR), Text.getString(Text.TEXT_DIALOG_FILL_COMPULSORY_DATA));
                        }
                    } else if(currentForm == cameraForm) {
                        switchForm(imageForm, false);
                    } else if(currentForm == imageForm) {
                        if(imageForm.continueable()) {
                            confirmationForm.updateTexts();
                            switchForm(confirmationForm, false);
                        } else {
                            showErrorDialog(Text.getString(Text.TEXT_GLOBAL_ERROR), Text.getString(Text.TEXT_DIALOG_TAKE_ONE_PICTURE));
                        }
                    }
                }

            };
        }

        return continueCommand;
    }

    public Command getExitCommand() {
        // Lazy initialization
        if(exitCommand == null) {
            exitCommand = new Command(Text.getString(Text.TEXT_GLOBAL_EXIT)) {

                public void actionPerformed(ActionEvent evt) {
                    exitMidlet();
                }

            };
        }

        return exitCommand;
    }

    public Command getTakePictureCommand() {
        // Lazy initialization
        if(takePictureCommand == null) {
            takePictureCommand = new Command(Text.getString(Text.TEXT_GLOBAL_TAKE_PICTURE)) {

                public void actionPerformed(ActionEvent evt) {
                    byte[] imageData = cameraForm.takePicture();
                    Image image = Image.createImage(imageData, 0, imageData.length);
                    imageForm.createImageTab(image);
                }

            };
        }

        return takePictureCommand;
    }

    public Command getSendCommand() {
        // Lazy initialization
        if(sendCommand == null) {
            sendCommand = new Command(Text.getString(Text.TEXT_GLOBAL_SEND)) {

                public void actionPerformed(ActionEvent evt) {
                    if(getCurrentForm() == confirmationForm) {
                        switchForm(sendForm, false);
                    }
                }

            };
        }

        return sendCommand;
    }

    public Command getNewCommand() {
        // Lazy initialization
        if(newCommand == null) {
            newCommand = new Command(Text.getString(Text.TEXT_GLOBAL_NEW)) {

                public void actionPerformed(ActionEvent evt) {
                    if(getCurrentForm() == sendForm) {
                        // Clear forms
                        clearForms();

                        switchForm(homeForm, true);
                    }
                }

            };
        }

        return newCommand;
    }

    public void clearForms() {
       getBasicForm().clearTexts();
       getImageForm().clearImages();
    }

    public void showErrorDialog(String title, String text) {
        Dialog.setDefaultDialogType(Dialog.TYPE_ERROR);
        Dialog.show(title, text, Text.getString(Text.TEXT_GLOBAL_OK), null);
    }

    public void showExitDialog(String title, String text) {
        Dialog.setDefaultDialogType(Dialog.TYPE_ERROR);
        Dialog.show(title, text, new Command[] {getExitCommand()}, Dialog.TYPE_ERROR, null, 0);
    }

    public void switchForm(Form form, boolean back) {
        if(back) {
            form.showBack();
        } else {
            form.show();
        }
    }

    public Form getCurrentForm() {
        return Display.getInstance().getCurrent();
    }

    public CommonTransitions getTransition() {
        return CommonTransitions.createSlide(DEFAULT_TRANSITION, false, DEFAULT_TRANSITION_DURATION);
    }
}
